package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.Farewell;
import com.ruoyi.system.mapper.FarewellMapper;
import com.ruoyi.system.service.IFarewellService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 午别Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class FarewellServiceImpl implements IFarewellService {
    @Autowired
    private FarewellMapper farewellMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询午别
     *
     * @param farewellId 午别主键
     * @return 午别
     */
    @Override
    public Farewell selectFarewellByFarewellId(Long farewellId) {
        return farewellMapper.selectFarewellByFarewellId(farewellId);
    }

    /**
     * 查询午别列表
     *
     * @param farewell 午别
     * @return 午别
     */
    @Override
    public List<Farewell> selectFarewellList(Farewell farewell) {
        return farewellMapper.selectFarewellList(farewell);
    }

    /**
     * 新增午别
     *
     * @param farewell 午别
     * @return 结果
     */
    @Override
    public int insertFarewell(Farewell farewell) {
        farewell.setCreateTime(DateUtils.getNowDate());
        return farewellMapper.insertFarewell(farewell);
    }

    /**
     * 修改午别
     *
     * @param farewell 午别
     * @return 结果
     */
    @Override
    public int updateFarewell(Farewell farewell) {
        return farewellMapper.updateFarewell(farewell);
    }

    /**
     * 批量删除午别
     *
     * @param farewellIds 需要删除的午别主键
     * @return 结果
     */
    @Override
    public int deleteFarewellByFarewellIds(Long[] farewellIds) {
        return farewellMapper.deleteFarewellByFarewellIds(farewellIds);
    }

    /**
     * 删除午别信息
     *
     * @param farewellId 午别主键
     * @return 结果
     */
    @Override
    public int deleteFarewellByFarewellId(Long farewellId) {
        return farewellMapper.deleteFarewellByFarewellId(farewellId);
    }

    /**
     * 实体数据补全
     *
     * @param farewell  实体
     * @param userId    用户id
     * @param crateType 创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(Farewell farewell, Long userId, Integer crateType) {
        //主键
        if (farewell.getFarewellId() == null || "".equals(farewell.getFarewellId())) {
            farewell.setFarewellId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (farewell.getCreateBy() == null) {
                farewell.setCreateBy(userId.toString());
            }
            if (farewell.getCreateTime() == null) {
                farewell.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (farewell.getModifyBy() == null) {
            farewell.setModifyBy(userId);
        }
        //修改时间
        if (farewell.getModifyTime() == null) {
            farewell.setModifyTime(DateUtils.getNowDate());
        }
    }

}
